package net.dgg.rz.production.controller;

import net.dgg.framework.tac.utils.bean.DggRestResponse;
import net.dgg.framework.tac.utils.exception.DggBaseException;
import net.dgg.framework.tac.utils.web.commmon.DggBaseController;
import net.dgg.iboss.base.util.CustomerNumberEncryptionUtil;
import net.dgg.iboss.base.util.DesUtil;
import net.dgg.rz.common.constant.ScProductConstant;
import net.dgg.rz.common.entity.dto.AllotRecordDto;
import net.dgg.rz.common.service.AllotRecordService;
import net.dgg.rz.common.service.CommonMangerExtService;
import net.dgg.rz.common.utils.DataTableResponse;
import net.dgg.rz.common.utils.ExcelTool;
import net.dgg.rz.common.utils.ExceptionUtils;
import net.dgg.rz.production.entity.dto.ProductOrderDto;
import net.dgg.rz.production.exception.ProductOrderExcption;
import net.dgg.rz.production.service.ProductOrderService;
import net.dgg.tmd.foundation.platform.common.util.TranslateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

/**
 * @className AllocateController
 * @Author Quanlin Wan
 * @Description //TODO 分配
 * @Date 2018/12/17 18:27
 **/
@Controller
@RequestMapping(value = "/rzsc/allocate")
public class AllocateController extends DggBaseController {
    private final static String PATH_PREFIX = "/rzsc/allocate";
    @Autowired
    private ProductOrderService productOrderService;
    @Autowired
    private AllotRecordService allotRecordService;
    /**
     * @Author Quanlin Wan
     * @Description //TODO 待分配页面
     * @Date 15:29 2018/12/18
     * @Param
     * @return
     **/

    @RequestMapping("/index.html")
    public String index(Model model){
        model.addAttribute("TIME_OUT", ScProductConstant.TIME_OUT);
        return "rzsc/allocation/allocate";
    }

    /**
     * @Author Quanlin Wan
     * @Description //TODO 待分配页面分页数据
     * @Date 15:29 2018/12/18
     * @Param
     * @return
     **/

    @RequestMapping("/list.do")
    @ResponseBody
    public DggRestResponse list(@RequestParam Map params) {
        try {
            List<ProductOrderDto> productOrderDtos = productOrderService.waitAllocateOrderWithPage(params);
            return new DataTableResponse().data(params, productOrderDtos);
        } catch (ProductOrderExcption e) {
            e.printStackTrace();
            return this.getFailResponse(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            ExceptionUtils.error(e, PATH_PREFIX+"/list.do", params.toString());
            return this.getFailResponse(ScProductConstant.SYS_ERROR);
        }

    }

    /**
     * @Author Quanlin Wan
     * @Description //TODO 已分配记录页面
     * @Date 15:29 2018/12/18
     * @Parama
     * @return
     **/

    @RequestMapping("/allocate_record_index.html")
    public String allocateRecordIndex(Model model){
        model.addAttribute("TIME_OUT", ScProductConstant.TIME_OUT);
        return "rzsc/allocation/allocate_record";
    }

    /**
     * @Author Quanlin Wan
     * @Description //TODO 已分配页面分页数据
     * @Date 15:29 2018/12/18
     * @Param
     * @return
     **/

    @RequestMapping("/allocate_record_list.do")
    @ResponseBody
    public DggRestResponse allocateRecordlist(@RequestParam Map params) {
        try {
            List<AllotRecordDto> allotRecordDtos = allotRecordService.allotRecordWithPage(params);
            return new DataTableResponse().data(params, allotRecordDtos);
        } catch (ProductOrderExcption e) {
            e.printStackTrace();
            return this.getFailResponse(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            ExceptionUtils.error(e, PATH_PREFIX+"/allocate_record_list.do", params.toString());
            return this.getFailResponse(ScProductConstant.SYS_ERROR);
        }

    }


    /**
     * @Author Quanlin Wan
     * @Description //TODO 打开分配页面
     * @Date 19:36 2018/12/25
     * @Param
     * @return
     **/

    @RequestMapping(value = "/allot_scorder_page.html")
    public String allotScOrderPage(Model model) {
        return "rzsc/allocation/allocate_dialog";
    }
    
    /**
     * @Author Quanlin Wan
     * @Description //TODO 分配
     * @Date 19:48 2018/12/25
     * @Param 
     * @return 
     **/

    @RequestMapping("/allocate_order.do")
    @ResponseBody
    public Object allocateOrder(@RequestParam String params) {
        try {
            this.productOrderService.allocateOrder(params);
            return this.getSuccessResponse("操作成功!");
        } catch (ProductOrderExcption e) {
            e.printStackTrace();
            return this.getFailResponse(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            ExceptionUtils.error(e, PATH_PREFIX+"/allocate_record_list.do", params);
            return this.getFailResponse(ScProductConstant.SYS_ERROR);
        }
    }


    /**
     * 导出(待分配)
     */
    @RequestMapping(value = "/excel_export_wait_allot.do")
    @ResponseBody
    public void exportDetailExcelAllot(@RequestParam Map params) {
        int[] widths = new int[] { 512*8, 512*8,512*8, 512*8,512*8, 512*8, 512*8, 512*8, 512*8, 512*8, 512*8, 512*8, 512*8, 512*8, 512*8, 512*8, 512*8, 512*8, 512*8, 512*8};
        int[] styleArr = new int[] { -1, 1,0, 1, 1,0 , 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1};
        List<ProductOrderDto> list = productOrderService.waitAllocateOrderWithPage(params);
        String sheetName  = "待分配生产订单数据";
        String[] title = new String[]{"生产订单编号", "订单编号","客户名称","客户电话","服务合同编号","所属事业部","业务类型","产品","渠道","签单时间","签单商务","签单人部门","需求金额"};
        String[] keyArray = new String[] {"scProductOrderNo","orderNo","customerName","customerPhone","contractNo","productOrgName","businessProductName","productName","channelName","orderTime","signUserName","signUserOrgName","loanAmount"};
        ExcelTool.execlExport(list,sheetName,title,keyArray,widths,styleArr);
    }

    /**
     * 导出(已分配记录)/
     */
    @RequestMapping(value = "/excel_export_wait_allot_record.do")
    @ResponseBody
    @SuppressWarnings("all")
    public void exportDetailExcelAllotRecord(@RequestParam Map params) {
        int[] widths = new int[] { 512*8, 512*8, 512*8, 512*8, 512*8, 512*8, 512*8, 512*8, 512*8, 512*8, 512*8, 512*8, 512*8, 512*8, 512*8, 512*8, 512*8, 512*8};
        int[] styleArr = new int[] { -1, 0, 1, 0 , 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1};
        List<AllotRecordDto> allotRecordDtos = allotRecordService.allotRecordWithPage(params);
        String sheetName  = "已分配生产订单数据";
        String[] title = new String[]{"生产订单编号", "订单编号", "签单时间","签单人","签单人部门","客户名称","客户电话","分配时间","需求金额","业务类型","产品","渠道" ,"所属事业部","流程人员","流程人员部门"};
        String[] keyArray = new String[] {"scProductOrderNo","orderNo","orderTime","signUserName","signUserOrgName","customerName","customerPhone","allotDate","loanAmount","businessProductName","productName","channelName","productOrgName","flowUserName","flowUserOrgName"};
        ExcelTool.execlExport(allotRecordDtos,sheetName,title,keyArray,widths,styleArr);
    }


}
